import json
import re
import time

import requests


class Spider:

    def __init__(self,url,code,save_folder):
        #分享链接
        self.url = url

        #提取码
        self.code = code

        #将内容保存的文件夹，提前新建好
        self.save_folder = save_folder

        #目前未实现登录接口，登录后获取cookie,这个时效性挺长，可自行时效登录接口
        # self.cookie = {
        #     'Cookie': 'BAIDUID=509BCA0492F089F6927D78D90F63F196:FG=1; BIDUPSID=509BCA0492F089F6927D78D90F63F196; PSTM=1587880826; PANWEB=1; MCITY=-%3A; BDUSS=wtYXBuV1UyQW1jM1BYQmZTaHRLLXhjajQzVjZRS2loZXVkMm54Q040M2NSdkJmSVFBQUFBJCQAAAAAAAAAAAEAAAAo7Fgsu8bGt7ryAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANy5yF~cuchfRU; BDUSS_BFESS=wtYXBuV1UyQW1jM1BYQmZTaHRLLXhjajQzVjZRS2loZXVkMm54Q040M2NSdkJmSVFBQUFBJCQAAAAAAAAAAAEAAAAo7Fgsu8bGt7ryAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANy5yF~cuchfRU; SCRC=792b00b9cf8da8ab9ea0449378bda933; STOKEN=70df83237953153049aa0a9434b84b2a717073db51a2bd725401258cbbbebe82; PANPSC=2557144466992800376%3ADJI9ZdfpjgIOdqpyNImO0es60cmUHWkVZ1Qv138E%2B2Cd3hYutLPSps%2B8cTghPksROA6%2BHHtciFc2BoWObJVtSVApSOnHlEzjUOuF9bNVtOXvuopL0N3Et3zuXyu7bA2%2BovmNV%2B7na%2FRtoLnfYC42LaarwdLUD535bXEHvm4MBGkwqPG3E9RzCg%3D%3D'
        # }
        self.cookie = {
            'Cookie':"BIDUPSID=84B6707801F5728D2B7EEE6508BF42BF; PSTM=1618294345; BAIDUID=84B6707801F5728DCA2D6B447C86C229:FG=1; __yjs_duid=1_2fddcd909e71af237904939e1e5e5b081618294951015; PANWEB=1; BDUSS=Wt5UVdtV3VBMTBDTVlXakhpcU1ob01CaDlYVjNSVkRFZzFnRzlCVWs4ZWM3cDFnSVFBQUFBJCQAAAAAAAAAAAEAAAAo7Fgsu8bGt7ryAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJxhdmCcYXZgO; BDUSS_BFESS=Wt5UVdtV3VBMTBDTVlXakhpcU1ob01CaDlYVjNSVkRFZzFnRzlCVWs4ZWM3cDFnSVFBQUFBJCQAAAAAAAAAAAEAAAAo7Fgsu8bGt7ryAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJxhdmCcYXZgO; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; STOKEN=f379a97aba8ef40c710653329832cf5dd6a86579904e86ccbb8e1f3e9ebce246; BDSFRCVID_BFESS=ip8OJexroG38OfOeCR41UZe8qSNbUdrTDYLtOwXPsp3LGJLVJeC6EG0Pts1-dEu-EHtdogKKKgOTHICF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tbCO_KDhtKD3DJjnq4cH-ntSqxby26nRfGO9aJ5nJD_MjRLz3pOqLpttDnozK6vD5j6l_q_aQpP-HJ7y2M6zKULf0fvJbq5K-J50Kl0MLInWbb0xynoDhqLEjMnMBMni52OnapTn3fAKftnOM46JehL3346-35543bRTLnLy5KJtMDFleP55q4D_MfOtetJyaR30QInvWJ5WqR7jDT7cbx48LnKJJlQz5RbDbhrtQRbKShbXXbOc5pkrKp0eWUbZBNcZ0l8K3l02V-bRDDcfQJQDQt7JKPRMW23i0h7mWpTTsxA45J7cM4IseboJLfT-0bc4KKJxbnLWeIJEjj6jK4JKDNuqqTbP; H_PS_PSSID=33839_33817_31254_34004_33675_33607_26350_33892; PSINO=5; BAIDUID_BFESS=8FD93EA23BBFC5CC79FC403DAF77FBCE:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=1; BA_HECTOR=058l212kal8k012lnq1gacqgv0r; csrfToken=2wAODpfv6R4SUNcRW4wxEiag; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1621250475,1621520076; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1621520076; PANPSC=7889413647144687363%3ADJI9ZdfpjgIOdqpyNImO0es60cmUHWkVZ1Qv138E%2B2Cd3hYutLPSps%2B8cTghPksROA6%2BHHtciFfTifYIhqCmcmJINVA3eHxSUOuF9bNVtOXvuopL0N3Et3zuXyu7bA2%2BovmNV%2B7na%2FRtoLnfYC42LaarwdLUD535bXEHvm4MBGkwqPG3E9RzCg%3D%3D; BDCLND=pZRAJh%2F1%2FJZYFLN2qjq6NEfJUAXU266zVqoNJdnKlmY%3D"
        }

        self.headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
    }

    def baidu_save_file(self):
        """
        主函数
        保存文件or文件夹到云盘文件夹
        1.判断是否需要解压，跳转到https://pan.baidu.com/share/init?surl=url页面
        2.校验
        2.获取我的文件夹信息
        3.保存数据
        :return:
        """
        # 请求输入提取码url post请求 param:pwd vcode vcode_str
        verify_pwd_url = 'https://pan.baidu.com/share/verify?surl={surl}&t=1606890120357&channel=chunlei&web=1&app_id=250528&bdstoken=00e970c0cbbc6d5d438a7b1aedf18df8&logid=NTA5QkNBMDQ5MkYwODlGNjkyN0Q3OEQ5MEY2M0YxOTY6Rkc9MQ==&clienttype=0'
        source_url = self.url
        result = re.findall('\/s\/([\s\S]*?)$', source_url)
        url = result[0] if result else False
        if not url:
            return False,'url解析失败'
        # 去除前面1位字符 1voH52xcY38EVGMBHaLvsag->voH52xcY38EVGMBHaLvsag
        surl = url[1:]
        data = {
            'pwd': self.code,
            'vcode': "",
            'vcode_str': ""
        }
        baidu_cookies = self.cookie
        headers = self.headers
        headers.update(baidu_cookies)
        headers.update({"Referer": "https://pan.baidu.com/share/init?surl=" + surl})
        # 后期决定这个randsk是否保存至redis里面
        response = requests.post(verify_pwd_url.format(surl=surl), data=data, headers=headers)
        if response.status_code == 200:
            result = json.loads(response.text)
            errno = result.get('errno')
            if errno == 0:
                # 返回参数randsk ，访问页面时使用cookie时使用
                cookie_BDCLND = result.get('randsk')
                headers.update({'Cookie': 'BDCLND=' + cookie_BDCLND + ';'})
                status, msg = self.__get_field_list(headers, source_url, self.save_folder)
                if not status:
                    return False,msg
                return True,msg
        return False,'提取码输入失败'

    def __get_field_list(self,headers, access_url, save_folder):
        result_info = requests.get(access_url, headers=headers)
        if result_info.status_code == 200:
            result_html = result_info.text
            print(result_html)
            result = self.__parse_result(result_html)
            if not result:
                return False, '访问失败，页面不存在'
            url = 'https://pan.baidu.com/share/transfer?shareid={shareid}&from={from1}&channel=chunlei&web=1&app_id={app_id}&bdstoken=00e970c0cbbc6d5d438a7b1aedf18df8&logid=NTA5QkNBMDQ5MkYwODlGNjkyN0Q3OEQ5MEY2M0YxOTY6Rkc9MQ==&clienttype=0'
            fsidlist = result['fsidlist']
            datas = {
                'path': '/扩展/p-p/' + save_folder,
                'fsidlist': json.dumps(fsidlist)
            }
            status, msg = self.__transfer_file(
                url.format(shareid=result['shareid'], from1=result['from'], app_id=result['app_id']), headers, datas,
                access_url)
            return status, result['file_name'] if status else msg
        return False, '请求获取js列表数据失败'

    def __parse_result(self,str):
        """
        解析数据
        :param str:
        :return:
        """
        content = str
        pattern = 'locals.mset\(({[\s\S]*?})\);'
        result = re.findall(pattern, content)

        result = json.loads(result[0]) if result else {}
        print(result)
        lists = result.get('file_list')
        if lists:
            values = lists
            fsidlist = []
            file_name = []
            for value in values:
                app_id = value['app_id']
                fsidlist.append(value['fs_id'])
                file_name.append(value['server_filename'])
            dict = {
                'shareid': result.get('shareid'),
                'from': result.get('uk'),
                'app_id': app_id,
                'fsidlist': fsidlist,
                'file_name': file_name,
            }
            return dict
        return False


    def __transfer_file(self,url, headers, payload, access_url):
        """
        保存文件
        :param url:
        :param headers:
        :param payload:
        :param access_url:
        :return:
        """
        headers.update({'Referer': access_url})
        headers.update({'Cookie': self.cookie.get('Cookie') + '; ' + headers.get('Cookie')})
        response = requests.post(url, headers=headers, data=payload)
        if response.status_code == 200:
            result = json.loads(response.text)
            print(result)
            if result['errno'] == 0:
                return True, '保存文件成功'
            if result['errno'] == 12:
                return False, '文件重复保存'
            return False, '保存文件失败未知异常' + str(result['errno'])
        return False, '保存文件至网盘接口请求失败'

url = 'https://pan.baidu.com/s/1ZODg6NWHYtEDzuyIGc3KlA'
code = '4lp7'
spider = Spider(url,code,'shuangyu')
status,msg = spider.baidu_save_file()
print(status,msg)

# str = 'locals.mset({"csrf":"At201ThA-q2nYLfSWgDHp-lVr8KvWqVFumx4","uk":"282335","username":"","loginstate":0,"skinName":"white","bdstoken":"","photo":"https://dgss0.bdstatic.com/6LZ1dD3d1sgCo2Kml5_Y_D3/sys/portrait/item/00000000","is_vip":0,"is_svip":0,"is_evip":0,"now":"2021-05-20T13:45:02.990Z","XDUSS":"","curr_activity_code":0,"show_vip_ad":0,"share_photo":"https://dss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/netdisk.1.2d703469.w8Ct7tTMvKWtOyz_pd7e7A.jpg","share_uk":"895397926","shareid":1809935642,"hit_ogc":false,"expiredType":0,"public":0,"ctime":1609145300,"description":"","followFlag":-1,"access_list_flag":true,"owner_vip_level":5,"owner_vip_type":2,"linkusername":"公众***","share_page_type":"multi","title_img":["iVBORw0KGgoAAAANSUhEUgAAARsAAAAeCAIAAACT9S1xAAAGEElEQVR42uycb0hTXxjHz53X2Aabo6zVMqjci0VCo4nrhRWtCIoQKxQLRJF1s2EUVLKBwSL7Q+ALyf4gTsZcOAr6Y/bCGtg/MtJWN51ZCC5HtVnSlrs1t/vnB95f93e5k35vetHg+bw6zzn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Vr1en1NTY3dbkcIHT58uLe31+FwnD9/HkIFZJ+iFApFcXEx356YmGAYRjBv3Lgh8XS73fn5+ZLO2dlZsUnTtMFg+PLlS0tLSyAQWLx4cX9/P03T826loaGBJMknT578d8jDsK6uLrPZjGHYuXPnIFpANilKJpPxFTnenJqaYhhGMBFCOTk5Yk+NRpOpqJ8/f4pzjkKhGBsbO378OEKos7PTarVSFOVyuUwmk0R4BEH4/f5nz56p1WrxkFarvX///saNG8fHx10ul2QUAP5eRYVCIfHXJ7VazTCMUIVzOBxTU1Nizx07dqxdu1ayXCwWu3PnDt+ORqOLFi3i2+Fw2Ov1lpaWhsPh7u7u9evXz8zM1NXVffz4MS8vr7m5ub+//9GjR/X19fw3KL4i0tHRgRAqKyt7+vTpzp07r127dujQIYgZkB2KoihKfFhKJpMMw4h7Fi5cKPZsbW3NzBjpdFpokyRpMBiE3GWxWAYHB1etWqXRaAYGBlQqVXFx8YEDB1Qqld1uJwhCp9PdvHmTv9908uRJhNDp06f53Jibm0uSpFKphIAB2aGotra28fFxSY2BZdlQKCROYtFodNmyZV+/fo1EIlVVVStXrpQsl0gknE6n2+3WarW3bt0yGo23b99+/fo1QRBnz57l5xiNxpcvXw4PDwt1Rblczt8YXLBggfgNUy6XCyuDnIBsUpTJZJLIY9u2bZmztVrtxMREIpFoaGjg9ZM5x+l0JhIJtVptsVhKSkpaWlrMZvOuXbuECaWlpRRFCdX2eVmyZAnEBshGMPhfSADwB4G/jwIAUBQAgKIAABQFAAAoCgBAUQAAigIAQMo/AQAA//8zHr+bXGvKrwAAAABJRU5ErkJggg=="],"file_list":[{"app_id":"250528","category":6,"delete_fs_id":"0","extent_int3":"895397926","extent_tinyint1":"0","extent_tinyint2":"0","extent_tinyint3":"0","extent_tinyint4":"0","file_key":"","fs_id":973845350148160,"isdelete":"0","isdir":1,"local_ctime":1609145275,"local_mtime":1609145275,"md5":"","oper_id":"895397926","parent_path":"%2Fsharelink895397926-973845350148160","path":"/sharelink895397926-973845350148160/重置脚本","pmd5":"3297951125545220895","privacy":"0","root_ns":1526281726,"server_atime":"0","server_ctime":1609145275,"server_filename":"重置脚本","server_mtime":1609145275,"share":"0","":0,"status":"0","tkbind_id":"0","videotag":"0","wpfile":"0"}],"errortype":-1,"errno":0,"self":0});'
#
# pattern = 'locals.mset\(({[\s\S]*?})\)'
# result = re.findall(pattern, str)
#
# result = json.loads(result[0]) if result else {}
#
# lists = result.get('file_list')
# print(lists)

